package com.cskaoyan.market.controller;

import com.cskaoyan.market.service.AdminConfigService;
import com.cskaoyan.market.service.impl.AdminConfigServiceImpl;
import com.cskaoyan.market.util.HttpUtil;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@WebServlet("/admin/config/*")
public class AdminConfigController extends HttpServlet {
    AdminConfigService adminConfigService = new AdminConfigServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String op = requestURI.replace(req.getContextPath() + "/admin/config/", "");
        if ("express".equals(op)) {
            getExpress(req, resp);
        } else if ("wx".equals(op)) {
            getWx(req, resp);
        } else if ("order".equals(op)) {
            getOrder(req, resp);
        }else if ("mall".equals(op)) {
            getMall(req, resp);
        }
    }

    private void getMall(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Map<String,String> mallMap = adminConfigService.mall();
        Object o = ResponseUtil.ok(mallMap);
        resp.getWriter().println(JacksonUtil.writeValueAsString(o));
    }

    private void getOrder(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Map<String,Object> map = adminConfigService.getOrderResponse();
        resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.ok(map)));
    }

    private void getWx(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Map<String,Object> map = adminConfigService.getWxResponse();
        resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.ok(map)));
    }

    private void getExpress(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Map<String,Object> map = adminConfigService.getExpressResponse();
        resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.ok(map)));
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String op = requestURI.replace(req.getContextPath() + "/admin/config/", "");
        if ("express".equals(op)) {
            postExpress(req, resp);
        } else if ("wx".equals(op)) {
            postWx(req, resp);
        } else if ("order".equals(op)) {
            postOrder(req, resp);
        }else if ("mall".equals(op)) {
            postMall(req, resp);
        }
    }

    private void postMall(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = req.getReader().readLine();
        String marketMallAddress = JacksonUtil.parseString(requestBody,"market_mall_address");
        String marketMallLatitude = JacksonUtil.parseString(requestBody, "market_mall_latitude");
        String marketMallLongitude = JacksonUtil.parseString(requestBody, "market_mall_longitude");
        String marketMallName = JacksonUtil.parseString(requestBody, "market_mall_name");
        String marketMallPhone = JacksonUtil.parseString(requestBody, "market_mall_phone");
        String marketMallQq = JacksonUtil.parseString(requestBody, "market_mall_qq");
        adminConfigService.postMall(marketMallAddress,marketMallLatitude,marketMallLongitude,marketMallName,marketMallPhone,marketMallQq);
        Object o = ResponseUtil.ok();
        resp.getWriter().println(JacksonUtil.writeValueAsString(o));
    }

    private void postOrder(HttpServletRequest req, HttpServletResponse resp) throws IOException{
        String requestBody = HttpUtil.getRequestBody(req);
        String comment = JacksonUtil.parseString(requestBody, "market_order_comment");
        String unconfirm = JacksonUtil.parseString(requestBody, "market_order_unconfirm");
        String unpaid = JacksonUtil.parseString(requestBody, "market_order_unpaid");
        adminConfigService.modifyOrder(comment,unconfirm,unpaid);
        resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.ok()));
    }

    private void postWx(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = HttpUtil.getRequestBody(req);
        String goods = JacksonUtil.parseString(requestBody, "market_wx_catlog_goods");
        String list = JacksonUtil.parseString(requestBody, "market_wx_catlog_list");
        String brand = JacksonUtil.parseString(requestBody, "market_wx_index_brand");
        String hot = JacksonUtil.parseString(requestBody, "market_wx_index_hot");
        String catlogNew = JacksonUtil.parseString(requestBody, "market_wx_index_new");
        String topic = JacksonUtil.parseString(requestBody, "market_wx_index_topic");
        String catlogFalse = JacksonUtil.parseString(requestBody, "market_wx_share");
        adminConfigService.modifyWx(goods,list,brand,hot,catlogNew,topic,catlogFalse);
        resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.ok()));
    }

    private void postExpress(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = HttpUtil.getRequestBody(req);
        String min = JacksonUtil.parseString(requestBody, "market_express_freight_min");
        String value = JacksonUtil.parseString(requestBody, "market_express_freight_value");
        adminConfigService.modifyExpress(min,value);
        resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.ok()));
    }
}
